package com.shopper.manager.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.shopper.manager.model.customer.req.RegisteredReq;
import com.shopper.manager.model.index.UserBean;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service("TokenService")
public class TokenService {

    /**
     * 商家端token处理
     * @param user
     * @return
     */
    public String getToken(UserBean user){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return JWT.create().withAudience(String.valueOf(user.getUserId()),df.format(new Date()),user.getAccount())// 将 user id 保存到 token 里面
              .sign(Algorithm.HMAC256(user.getBusinessLicense()));
    }


    /**
     * 客户端token处理
     * @param req
     * @return
     */
    public String getCustomerToken(RegisteredReq req){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return JWT.create().withAudience(String.valueOf(req.getId()),df.format(new Date()),req.getCustomerAccount())// 将 user id 保存到 token 里面
                .sign(Algorithm.HMAC256(req.getBusinessLicense()));
    }
}
